package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDetailDTO;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.updateEmpParam;
import com.itheima.tlias84.servrce.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("emps")
public class EmpController {
    @Resource
    EmpService empService;

    @GetMapping
    public ResultUtil selectEmpByPage(SelectEmpPageParam param){
        if(Objects.isNull(param.getPage())||Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }
        return empService.selectEmpByPage(param);
    }

    @DeleteMapping
    public ResultUtil deleteEmpById(@RequestParam("ids") List<Integer> ids) {
        if(ids.isEmpty()){
            return ResultUtil.fail("请先选择id");
        }
        int i = empService.deleteEmpById(ids);
        return  ResultUtil.success(i);
    }

    @PostMapping
    //@Validated                校验启动注解
    public ResultUtil insertEmp(@Validated  @RequestBody InsertEmpParam param) {
        if(Objects.isNull(param.getUsername())){}
        int i = empService.insertEmp(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @GetMapping("{id}")
    public ResultUtil selectEmpById(@PathVariable("id") Integer id){
        SelectEmpDetailDTO dto = empService.selectEmpById(id);
        return ResultUtil.success(dto);
    }

    @PutMapping
   public ResultUtil updateEmp(@RequestBody updateEmpParam param){
        int i = empService.updateEmp(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @GetMapping("list")
    public ResultUtil selectEmpList(){
        List<SelectEmpDTO> list = empService.selectEmpList();
        return ResultUtil.success(list);
    }


}
